//
//  MeShareQRView.m
//  Ev_Master
//
//  Created by Evan on 2019/7/7.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "MeShareQRView.h"

@implementation MeShareQRView

-(void)awakeFromNib{
    [super awakeFromNib];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
    [self addGestureRecognizer:tap];
}

-(void)viewClick:(id)sender{
    self.hidden = YES;
}

- (IBAction)btnCloseClick:(id)sender {
    self.hidden = YES;
}



-(void)initQRdata:(NSString*)strQr{
    self.imgQRCode.backgroundColor = [UIColor clearColor];
    
    // 1. 创建一个二维码滤镜实例(CIFilter)
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 滤镜恢复默认设置
    [filter setDefaults];
    
    // 2. 给滤镜添加数据
    //    NSString *string = @"学号:3180807001 \n  姓名:黄振锋";
    NSData *data = [strQr dataUsingEncoding:NSUTF8StringEncoding];
    // 使用KVC的方式给filter赋值
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 3. 生成二维码
    CIImage *image = [filter outputImage];
    
    //4.在中心增加一张图片
    UIImage *img = [self createNonInterpolatedUIImageFormCIImage:image withSize:self.imgQRCode.frame.size.width];
    
    //5.把中央图片划入二维码里面
    //5.1开启图形上下文
    UIGraphicsBeginImageContext(img.size);
    //5.2将二维码的图片画入
    [img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
    UIImage *centerImg = [UIImage imageNamed:@"HZhenF"];
    CGFloat centerW=img.size.width*0.3;
    CGFloat centerH=centerW;
    CGFloat centerX=(img.size.width-centerW)*0.5;
    CGFloat centerY=(img.size.height -centerH)*0.5;
    [centerImg drawInRect:CGRectMake(centerX, centerY, centerW, centerH)];
    //5.3获取绘制好的图片
    UIImage *finalImg=UIGraphicsGetImageFromCurrentImageContext();
    //5.4关闭图像上下文
    UIGraphicsEndImageContext();
    
    //6.显示最终二维码
    self.imgQRCode.image = finalImg;
    //    ---------------------
    //    作者：喜欢吃布丁但是没有钱
    //    来源：CSDN
    //    原文：https://blog.csdn.net/feng512275/article/details/82824650
    //    版权声明：本文为博主原创文章，转载请附上博文链接！
}


/**
 *  调用该方法处理图像变清晰
 *  根据CIImage生成指定大小的UIImage
 *
 *  @param image CIImage
 *  @param size  图片宽度以及高度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    //1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    //2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

@end
